package zhr.chapter9;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.util.Arrays;

public class TextListener implements DocumentListener {
    JTextArea inputText,showText;
    public void setInputText(JTextArea text) {
        inputText = text;
    }
    public void setShowText(JTextArea text) {
        showText = text;
    }
    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        String str = inputText.getText();
        //空格，数字和符号组成的正则表达式
        String regex="[\\s\\d\\p{Punct}]+";
        //匹配一下字符任意多个(大于或等于一个)
        //1.任意空白(空格、换行等)
        //2.任意数字(0-9)
        //3.p P u n c t { } 表示任意标点符号(此处的\\p并不是转移字符)
        String[] words = str.split(regex);
        Arrays.sort(words);
        showText.setText(null);
        for (String word : words) {
            showText.append(word + ",");
        }
    }
}
